# pulmonary_bc.py

from __future__ import with_statement
import itertools
from pyke import contexts, pattern, bc_rule

pyke_version = '1.1.1'
compiler_version = 1

def common_cold(rule, arg_patterns, arg_context):
  engine = rule.rule_base.engine
  patterns = rule.goal_arg_patterns()
  if len(arg_patterns) == len(patterns):
    context = contexts.bc_context(rule)
    try:
      if all(itertools.imap(lambda pat, arg:
                              pat.match_pattern(context, context,
                                                arg, arg_context),
                            patterns,
                            arg_patterns)):
        rule.rule_base.num_bc_rules_matched += 1
        with engine.prove(rule.rule_base.root_name, 'score', context,
                          (rule.pattern(0),
                           rule.pattern(1),
                           rule.pattern(2),)) \
          as gen_1:
          for x_1 in gen_1:
            assert x_1 is None, \
              "pulmonary.common_cold: got unexpected plan from when clause 1"
            with engine.prove(rule.rule_base.root_name, 'score', context,
                              (rule.pattern(3),
                               rule.pattern(1),
                               rule.pattern(4),)) \
              as gen_2:
              for x_2 in gen_2:
                assert x_2 is None, \
                  "pulmonary.common_cold: got unexpected plan from when clause 2"
                with engine.prove(rule.rule_base.root_name, 'score', context,
                                  (rule.pattern(5),
                                   rule.pattern(1),
                                   rule.pattern(6),)) \
                  as gen_3:
                  for x_3 in gen_3:
                    assert x_3 is None, \
                      "pulmonary.common_cold: got unexpected plan from when clause 3"
                    with engine.prove(rule.rule_base.root_name, 'score', context,
                                      (rule.pattern(7),
                                       rule.pattern(8),
                                       rule.pattern(9),)) \
                      as gen_4:
                      for x_4 in gen_4:
                        assert x_4 is None, \
                          "pulmonary.common_cold: got unexpected plan from when clause 4"
                        with engine.prove(rule.rule_base.root_name, 'score', context,
                                          (rule.pattern(10),
                                           rule.pattern(11),
                                           rule.pattern(12),)) \
                          as gen_5:
                          for x_5 in gen_5:
                            assert x_5 is None, \
                              "pulmonary.common_cold: got unexpected plan from when clause 5"
                            with engine.prove(rule.rule_base.root_name, 'score', context,
                                              (rule.pattern(13),
                                               rule.pattern(1),
                                               rule.pattern(14),)) \
                              as gen_6:
                              for x_6 in gen_6:
                                assert x_6 is None, \
                                  "pulmonary.common_cold: got unexpected plan from when clause 6"
                                mark7 = context.mark(True)
                                if rule.pattern(15).match_data(context, context,
                                        sum_cf(context.lookup_data('ans1'), context.lookup_data('ans2'), context.lookup_data('ans3'), context.lookup_data('ans4'), context.lookup_data('ans5'), context.lookup_data('ans6'))):
                                  context.end_save_all_undo()
                                  if context.lookup_data('ans')[0] >= cf_threshold        :
                                    rule.rule_base.num_bc_rule_successes += 1
                                    yield
                                else: context.end_save_all_undo()
                                context.undo_to_mark(mark7)
        rule.rule_base.num_bc_rule_failures += 1
    finally:
      context.done()

def allergic_rhinitis_1(rule, arg_patterns, arg_context):
  engine = rule.rule_base.engine
  patterns = rule.goal_arg_patterns()
  if len(arg_patterns) == len(patterns):
    context = contexts.bc_context(rule)
    try:
      if all(itertools.imap(lambda pat, arg:
                              pat.match_pattern(context, context,
                                                arg, arg_context),
                            patterns,
                            arg_patterns)):
        rule.rule_base.num_bc_rules_matched += 1
        with engine.prove(rule.rule_base.root_name, 'score', context,
                          (rule.pattern(0),
                           rule.pattern(1),
                           rule.pattern(2),)) \
          as gen_1:
          for x_1 in gen_1:
            assert x_1 is None, \
              "pulmonary.allergic_rhinitis_1: got unexpected plan from when clause 1"
            with engine.prove(rule.rule_base.root_name, 'score', context,
                              (rule.pattern(3),
                               rule.pattern(1),
                               rule.pattern(4),)) \
              as gen_2:
              for x_2 in gen_2:
                assert x_2 is None, \
                  "pulmonary.allergic_rhinitis_1: got unexpected plan from when clause 2"
                with engine.prove(rule.rule_base.root_name, 'score', context,
                                  (rule.pattern(5),
                                   rule.pattern(1),
                                   rule.pattern(6),)) \
                  as gen_3:
                  for x_3 in gen_3:
                    assert x_3 is None, \
                      "pulmonary.allergic_rhinitis_1: got unexpected plan from when clause 3"
                    with engine.prove(rule.rule_base.root_name, 'score', context,
                                      (rule.pattern(7),
                                       rule.pattern(8),
                                       rule.pattern(9),)) \
                      as gen_4:
                      for x_4 in gen_4:
                        assert x_4 is None, \
                          "pulmonary.allergic_rhinitis_1: got unexpected plan from when clause 4"
                        with engine.prove(rule.rule_base.root_name, 'score', context,
                                          (rule.pattern(10),
                                           rule.pattern(11),
                                           rule.pattern(12),)) \
                          as gen_5:
                          for x_5 in gen_5:
                            assert x_5 is None, \
                              "pulmonary.allergic_rhinitis_1: got unexpected plan from when clause 5"
                            with engine.prove(rule.rule_base.root_name, 'score', context,
                                              (rule.pattern(13),
                                               rule.pattern(14),
                                               rule.pattern(15),)) \
                              as gen_6:
                              for x_6 in gen_6:
                                assert x_6 is None, \
                                  "pulmonary.allergic_rhinitis_1: got unexpected plan from when clause 6"
                                mark7 = context.mark(True)
                                if rule.pattern(16).match_data(context, context,
                                        sum_cf(context.lookup_data('ans1'), context.lookup_data('ans2'), context.lookup_data('ans3'), context.lookup_data('ans4'), context.lookup_data('ans5'), context.lookup_data('ans6'))):
                                  context.end_save_all_undo()
                                  if context.lookup_data('ans')[0] >= cf_threshold        :
                                    rule.rule_base.num_bc_rule_successes += 1
                                    yield
                                else: context.end_save_all_undo()
                                context.undo_to_mark(mark7)
        rule.rule_base.num_bc_rule_failures += 1
    finally:
      context.done()

def allergic_rhinitis_2(rule, arg_patterns, arg_context):
  engine = rule.rule_base.engine
  patterns = rule.goal_arg_patterns()
  if len(arg_patterns) == len(patterns):
    context = contexts.bc_context(rule)
    try:
      if all(itertools.imap(lambda pat, arg:
                              pat.match_pattern(context, context,
                                                arg, arg_context),
                            patterns,
                            arg_patterns)):
        rule.rule_base.num_bc_rules_matched += 1
        with engine.prove(rule.rule_base.root_name, 'score', context,
                          (rule.pattern(0),
                           rule.pattern(1),
                           rule.pattern(2),)) \
          as gen_1:
          for x_1 in gen_1:
            assert x_1 is None, \
              "pulmonary.allergic_rhinitis_2: got unexpected plan from when clause 1"
            with engine.prove(rule.rule_base.root_name, 'score', context,
                              (rule.pattern(3),
                               rule.pattern(1),
                               rule.pattern(4),)) \
              as gen_2:
              for x_2 in gen_2:
                assert x_2 is None, \
                  "pulmonary.allergic_rhinitis_2: got unexpected plan from when clause 2"
                mark3 = context.mark(True)
                if rule.pattern(5).match_data(context, context,
                        sum_cf(context.lookup_data('ans1'), context.lookup_data('ans2'))):
                  context.end_save_all_undo()
                  if context.lookup_data('ans')[0] >= cf_threshold        :
                    rule.rule_base.num_bc_rule_successes += 1
                    yield
                else: context.end_save_all_undo()
                context.undo_to_mark(mark3)
        rule.rule_base.num_bc_rule_failures += 1
    finally:
      context.done()

def perrenial_rhinitis(rule, arg_patterns, arg_context):
  engine = rule.rule_base.engine
  patterns = rule.goal_arg_patterns()
  if len(arg_patterns) == len(patterns):
    context = contexts.bc_context(rule)
    try:
      if all(itertools.imap(lambda pat, arg:
                              pat.match_pattern(context, context,
                                                arg, arg_context),
                            patterns,
                            arg_patterns)):
        rule.rule_base.num_bc_rules_matched += 1
        with engine.prove(rule.rule_base.root_name, 'score', context,
                          (rule.pattern(0),
                           rule.pattern(1),
                           rule.pattern(2),)) \
          as gen_1:
          for x_1 in gen_1:
            assert x_1 is None, \
              "pulmonary.perrenial_rhinitis: got unexpected plan from when clause 1"
            with engine.prove(rule.rule_base.root_name, 'score', context,
                              (rule.pattern(3),
                               rule.pattern(1),
                               rule.pattern(4),)) \
              as gen_2:
              for x_2 in gen_2:
                assert x_2 is None, \
                  "pulmonary.perrenial_rhinitis: got unexpected plan from when clause 2"
                with engine.prove(rule.rule_base.root_name, 'score', context,
                                  (rule.pattern(5),
                                   rule.pattern(1),
                                   rule.pattern(6),)) \
                  as gen_3:
                  for x_3 in gen_3:
                    assert x_3 is None, \
                      "pulmonary.perrenial_rhinitis: got unexpected plan from when clause 3"
                    with engine.prove(rule.rule_base.root_name, 'score', context,
                                      (rule.pattern(7),
                                       rule.pattern(8),
                                       rule.pattern(9),)) \
                      as gen_4:
                      for x_4 in gen_4:
                        assert x_4 is None, \
                          "pulmonary.perrenial_rhinitis: got unexpected plan from when clause 4"
                        mark5 = context.mark(True)
                        if rule.pattern(10).match_data(context, context,
                                sum_cf(context.lookup_data('ans1'), context.lookup_data('ans2'), context.lookup_data('ans3'), context.lookup_data('ans4'))):
                          context.end_save_all_undo()
                          if context.lookup_data('ans')[0] >= cf_threshold        :
                            rule.rule_base.num_bc_rule_successes += 1
                            yield
                        else: context.end_save_all_undo()
                        context.undo_to_mark(mark5)
        rule.rule_base.num_bc_rule_failures += 1
    finally:
      context.done()

def allergic_perrenial_rhintis(rule, arg_patterns, arg_context):
  engine = rule.rule_base.engine
  patterns = rule.goal_arg_patterns()
  if len(arg_patterns) == len(patterns):
    context = contexts.bc_context(rule)
    try:
      if all(itertools.imap(lambda pat, arg:
                              pat.match_pattern(context, context,
                                                arg, arg_context),
                            patterns,
                            arg_patterns)):
        rule.rule_base.num_bc_rules_matched += 1
        with engine.prove(rule.rule_base.root_name, 'diagnosis', context,
                          (rule.pattern(0),
                           rule.pattern(1),)) \
          as gen_1:
          for x_1 in gen_1:
            assert x_1 is None, \
              "pulmonary.allergic_perrenial_rhintis: got unexpected plan from when clause 1"
            with engine.prove(rule.rule_base.root_name, 'score', context,
                              (rule.pattern(2),
                               rule.pattern(3),
                               rule.pattern(1),)) \
              as gen_2:
              for x_2 in gen_2:
                assert x_2 is None, \
                  "pulmonary.allergic_perrenial_rhintis: got unexpected plan from when clause 2"
                mark3 = context.mark(True)
                if rule.pattern(4).match_data(context, context,
                        sum_cf(context.lookup_data('ans1'))):
                  context.end_save_all_undo()
                  mark4 = context.mark(True)
                  if rule.pattern(5).match_data(context, context,
                          mult_cf(context.lookup_data('ans1'), context.lookup_data('sum'))):
                    context.end_save_all_undo()
                    rule.rule_base.num_bc_rule_successes += 1
                    yield
                  else: context.end_save_all_undo()
                  context.undo_to_mark(mark4)
                else: context.end_save_all_undo()
                context.undo_to_mark(mark3)
        rule.rule_base.num_bc_rule_failures += 1
    finally:
      context.done()

def non_allergic_perrenial_rhinitis(rule, arg_patterns, arg_context):
  engine = rule.rule_base.engine
  patterns = rule.goal_arg_patterns()
  if len(arg_patterns) == len(patterns):
    context = contexts.bc_context(rule)
    try:
      if all(itertools.imap(lambda pat, arg:
                              pat.match_pattern(context, context,
                                                arg, arg_context),
                            patterns,
                            arg_patterns)):
        rule.rule_base.num_bc_rules_matched += 1
        with engine.prove(rule.rule_base.root_name, 'diagnosis', context,
                          (rule.pattern(0),
                           rule.pattern(1),)) \
          as gen_1:
          for x_1 in gen_1:
            assert x_1 is None, \
              "pulmonary.non_allergic_perrenial_rhinitis: got unexpected plan from when clause 1"
            with engine.prove(rule.rule_base.root_name, 'score', context,
                              (rule.pattern(2),
                               rule.pattern(3),
                               rule.pattern(1),)) \
              as gen_2:
              for x_2 in gen_2:
                assert x_2 is None, \
                  "pulmonary.non_allergic_perrenial_rhinitis: got unexpected plan from when clause 2"
                with engine.prove(rule.rule_base.root_name, 'score', context,
                                  (rule.pattern(4),
                                   rule.pattern(5),
                                   rule.pattern(6),)) \
                  as gen_3:
                  for x_3 in gen_3:
                    assert x_3 is None, \
                      "pulmonary.non_allergic_perrenial_rhinitis: got unexpected plan from when clause 3"
                    with engine.prove(rule.rule_base.root_name, 'score', context,
                                      (rule.pattern(7),
                                       rule.pattern(3),
                                       rule.pattern(8),)) \
                      as gen_4:
                      for x_4 in gen_4:
                        assert x_4 is None, \
                          "pulmonary.non_allergic_perrenial_rhinitis: got unexpected plan from when clause 4"
                        mark5 = context.mark(True)
                        if rule.pattern(9).match_data(context, context,
                                sum_cf(context.lookup_data('ans1'), context.lookup_data('ans2'), context.lookup_data('ans3'))):
                          context.end_save_all_undo()
                          mark6 = context.mark(True)
                          if rule.pattern(10).match_data(context, context,
                                  mult_cf(context.lookup_data('ans1'), context.lookup_data('sum'))):
                            context.end_save_all_undo()
                            rule.rule_base.num_bc_rule_successes += 1
                            yield
                          else: context.end_save_all_undo()
                          context.undo_to_mark(mark6)
                        else: context.end_save_all_undo()
                        context.undo_to_mark(mark5)
        rule.rule_base.num_bc_rule_failures += 1
    finally:
      context.done()

def vasomotor_rhinitis(rule, arg_patterns, arg_context):
  engine = rule.rule_base.engine
  patterns = rule.goal_arg_patterns()
  if len(arg_patterns) == len(patterns):
    context = contexts.bc_context(rule)
    try:
      if all(itertools.imap(lambda pat, arg:
                              pat.match_pattern(context, context,
                                                arg, arg_context),
                            patterns,
                            arg_patterns)):
        rule.rule_base.num_bc_rules_matched += 1
        with engine.prove(rule.rule_base.root_name, 'diagnosis', context,
                          (rule.pattern(0),
                           rule.pattern(1),)) \
          as gen_1:
          for x_1 in gen_1:
            assert x_1 is None, \
              "pulmonary.vasomotor_rhinitis: got unexpected plan from when clause 1"
            with engine.prove(rule.rule_base.root_name, 'score', context,
                              (rule.pattern(2),
                               rule.pattern(3),
                               rule.pattern(1),)) \
              as gen_2:
              for x_2 in gen_2:
                assert x_2 is None, \
                  "pulmonary.vasomotor_rhinitis: got unexpected plan from when clause 2"
                with engine.prove(rule.rule_base.root_name, 'score', context,
                                  (rule.pattern(4),
                                   rule.pattern(5),
                                   rule.pattern(6),)) \
                  as gen_3:
                  for x_3 in gen_3:
                    assert x_3 is None, \
                      "pulmonary.vasomotor_rhinitis: got unexpected plan from when clause 3"
                    mark4 = context.mark(True)
                    if rule.pattern(7).match_data(context, context,
                            sum_cf(context.lookup_data('ans1'), context.lookup_data('ans2'))):
                      context.end_save_all_undo()
                      mark5 = context.mark(True)
                      if rule.pattern(8).match_data(context, context,
                              mult_cf(context.lookup_data('ans1'), context.lookup_data('sum'))):
                        context.end_save_all_undo()
                        rule.rule_base.num_bc_rule_successes += 1
                        yield
                      else: context.end_save_all_undo()
                      context.undo_to_mark(mark5)
                    else: context.end_save_all_undo()
                    context.undo_to_mark(mark4)
        rule.rule_base.num_bc_rule_failures += 1
    finally:
      context.done()

def score_answered(rule, arg_patterns, arg_context):
  engine = rule.rule_base.engine
  patterns = rule.goal_arg_patterns()
  if len(arg_patterns) == len(patterns):
    context = contexts.bc_context(rule)
    try:
      if all(itertools.imap(lambda pat, arg:
                              pat.match_pattern(context, context,
                                                arg, arg_context),
                            patterns,
                            arg_patterns)):
        rule.rule_base.num_bc_rules_matched += 1
        with engine.prove('question', context.lookup_data('question'), context,
                          (rule.pattern(0),)) \
          as gen_1:
          for x_1 in gen_1:
            assert x_1 is None, \
              "pulmonary.score_answered: got unexpected plan from when clause 1"
            with engine.prove('special', 'claim_goal', context,
                              ()) \
              as gen_2:
              for x_2 in gen_2:
                assert x_2 is None, \
                  "pulmonary.score_answered: got unexpected plan from when clause 2"
                max_score = 0.0
                score = 0.0
                for ans, ans_score in context.lookup_data('answers'):
                    if ans_score > max_score: max_score = ans_score
                    if ans == context.lookup_data('ans'): score = ans_score
                mark4 = context.mark(True)
                if rule.pattern(1).match_data(context, context,
                        max_score):
                  context.end_save_all_undo()
                  mark5 = context.mark(True)
                  if rule.pattern(2).match_data(context, context,
                          score):
                    context.end_save_all_undo()
                    rule.rule_base.num_bc_rule_successes += 1
                    yield
                  else: context.end_save_all_undo()
                  context.undo_to_mark(mark5)
                else: context.end_save_all_undo()
                context.undo_to_mark(mark4)
        rule.rule_base.num_bc_rule_failures += 1
    finally:
      context.done()

def score_unanswered(rule, arg_patterns, arg_context):
  engine = rule.rule_base.engine
  patterns = rule.goal_arg_patterns()
  if len(arg_patterns) == len(patterns):
    context = contexts.bc_context(rule)
    try:
      if all(itertools.imap(lambda pat, arg:
                              pat.match_pattern(context, context,
                                                arg, arg_context),
                            patterns,
                            arg_patterns)):
        rule.rule_base.num_bc_rules_matched += 1
        mark1 = context.mark(True)
        if rule.pattern(0).match_data(context, context,
                max(ans_score for ans, ans_score in context.lookup_data('answers'))):
          context.end_save_all_undo()
          rule.rule_base.num_bc_rule_successes += 1
          yield
        else: context.end_save_all_undo()
        context.undo_to_mark(mark1)
        rule.rule_base.num_bc_rule_failures += 1
    finally:
      context.done()

def score_not_match(rule, arg_patterns, arg_context):
  engine = rule.rule_base.engine
  patterns = rule.goal_arg_patterns()
  if len(arg_patterns) == len(patterns):
    context = contexts.bc_context(rule)
    try:
      if all(itertools.imap(lambda pat, arg:
                              pat.match_pattern(context, context,
                                                arg, arg_context),
                            patterns,
                            arg_patterns)):
        rule.rule_base.num_bc_rules_matched += 1
        with engine.prove('question', context.lookup_data('question'), context,
                          (rule.pattern(0),)) \
          as gen_1:
          for x_1 in gen_1:
            assert x_1 is None, \
              "pulmonary.score_not_match: got unexpected plan from when clause 1"
            with engine.prove('special', 'claim_goal', context,
                              ()) \
              as gen_2:
              for x_2 in gen_2:
                assert x_2 is None, \
                  "pulmonary.score_not_match: got unexpected plan from when clause 2"
                rule.rule_base.num_bc_rule_successes += 1
                yield
        rule.rule_base.num_bc_rule_failures += 1
    finally:
      context.done()

def score_not_no_match(rule, arg_patterns, arg_context):
  engine = rule.rule_base.engine
  patterns = rule.goal_arg_patterns()
  if len(arg_patterns) == len(patterns):
    context = contexts.bc_context(rule)
    try:
      if all(itertools.imap(lambda pat, arg:
                              pat.match_pattern(context, context,
                                                arg, arg_context),
                            patterns,
                            arg_patterns)):
        rule.rule_base.num_bc_rules_matched += 1
        with engine.prove('question', context.lookup_data('question'), context,
                          (rule.pattern(0),)) \
          as gen_1:
          for x_1 in gen_1:
            assert x_1 is None, \
              "pulmonary.score_not_no_match: got unexpected plan from when clause 1"
            with engine.prove('special', 'claim_goal', context,
                              ()) \
              as gen_2:
              for x_2 in gen_2:
                assert x_2 is None, \
                  "pulmonary.score_not_no_match: got unexpected plan from when clause 2"
                rule.rule_base.num_bc_rule_successes += 1
                yield
        rule.rule_base.num_bc_rule_failures += 1
    finally:
      context.done()

def score_not_unanswered(rule, arg_patterns, arg_context):
  engine = rule.rule_base.engine
  patterns = rule.goal_arg_patterns()
  if len(arg_patterns) == len(patterns):
    context = contexts.bc_context(rule)
    try:
      if all(itertools.imap(lambda pat, arg:
                              pat.match_pattern(context, context,
                                                arg, arg_context),
                            patterns,
                            arg_patterns)):
        rule.rule_base.num_bc_rules_matched += 1
        rule.rule_base.num_bc_rule_successes += 1
        yield
        rule.rule_base.num_bc_rule_failures += 1
    finally:
      context.done()

def populate(engine):
  This_rule_base = engine.get_create('pulmonary')
  
  bc_rule.bc_rule('common_cold', This_rule_base, 'diagnosis',
                  common_cold, None,
                  (pattern.pattern_literal('common_cold'),
                   contexts.variable('ans'),),
                  (),
                  (pattern.pattern_literal('runny_nose'),
                   pattern.pattern_literal((('yes', 0.8,),)),
                   contexts.variable('ans1'),
                   pattern.pattern_literal('nasal_blockage'),
                   contexts.variable('ans2'),
                   pattern.pattern_literal('sneezing'),
                   contexts.variable('ans3'),
                   pattern.pattern_literal('nasal_secretions'),
                   pattern.pattern_literal((('thick_purulent', 0.8,),)),
                   contexts.variable('ans4'),
                   pattern.pattern_literal('fever'),
                   pattern.pattern_literal((('low_grade', 0.6,),)),
                   contexts.variable('ans5'),
                   pattern.pattern_literal('malaise'),
                   contexts.variable('ans6'),
                   contexts.variable('ans'),))
  
  bc_rule.bc_rule('allergic_rhinitis_1', This_rule_base, 'diagnosis',
                  allergic_rhinitis_1, None,
                  (pattern.pattern_literal('allergic_rhinitis'),
                   contexts.variable('ans'),),
                  (),
                  (pattern.pattern_literal('runny_nose'),
                   pattern.pattern_literal((('yes', 0.8,),)),
                   contexts.variable('ans1'),
                   pattern.pattern_literal('nasal_blockage'),
                   contexts.variable('ans2'),
                   pattern.pattern_literal('sneezing'),
                   contexts.variable('ans3'),
                   pattern.pattern_literal('nasal_secretions'),
                   pattern.pattern_literal((('thin_watery', 0.6,),)),
                   contexts.variable('ans4'),
                   pattern.pattern_literal('on_exposure'),
                   pattern.pattern_literal((('dust', 0.9,),)),
                   contexts.variable('ans5'),
                   pattern.pattern_literal('trigger'),
                   pattern.pattern_literal((('smoke', 0.6,),)),
                   contexts.variable('ans6'),
                   contexts.variable('ans'),))
  
  bc_rule.bc_rule('allergic_rhinitis_2', This_rule_base, 'diagnosis',
                  allergic_rhinitis_2, None,
                  (pattern.pattern_literal('allergic_rhinitis'),
                   contexts.variable('ans'),),
                  (),
                  (pattern.pattern_literal('skin_test_for_allergen'),
                   pattern.pattern_literal((('positive', 0.9,),)),
                   contexts.variable('ans1'),
                   pattern.pattern_literal('eosinophils_in_secretions'),
                   contexts.variable('ans2'),
                   contexts.variable('ans'),))
  
  bc_rule.bc_rule('perrenial_rhinitis', This_rule_base, 'diagnosis',
                  perrenial_rhinitis, None,
                  (pattern.pattern_literal('perrenial_rhinitis'),
                   contexts.variable('ans'),),
                  (),
                  (pattern.pattern_literal('runny_nose'),
                   pattern.pattern_literal((('yes', 0.8,),)),
                   contexts.variable('ans1'),
                   pattern.pattern_literal('nasal_blockage'),
                   contexts.variable('ans2'),
                   pattern.pattern_literal('sneezing'),
                   contexts.variable('ans3'),
                   pattern.pattern_literal('cold_duration'),
                   pattern.pattern_literal((('more_than_2weeks', 0.9,),)),
                   contexts.variable('ans4'),
                   contexts.variable('ans'),))
  
  bc_rule.bc_rule('allergic_perrenial_rhintis', This_rule_base, 'diagnosis',
                  allergic_perrenial_rhintis, None,
                  (pattern.pattern_literal('allergic_perrenial_rhinitis'),
                   contexts.variable('ans'),),
                  (),
                  (pattern.pattern_literal('allergic_rhinitis'),
                   contexts.variable('ans1'),
                   pattern.pattern_literal('cold_duration'),
                   pattern.pattern_literal((('more_than_2weeks', 0.9,),)),
                   contexts.variable('sum'),
                   contexts.variable('ans'),))
  
  bc_rule.bc_rule('non_allergic_perrenial_rhinitis', This_rule_base, 'diagnosis',
                  non_allergic_perrenial_rhinitis, None,
                  (pattern.pattern_literal('non_allergic_perrenial_rhinitis'),
                   contexts.variable('ans'),),
                  (),
                  (pattern.pattern_literal('perrenial_rhinitis'),
                   contexts.variable('ans1'),
                   pattern.pattern_literal('skin_test_for_allergen'),
                   pattern.pattern_literal((('negative', 0.9,),)),
                   pattern.pattern_literal('trigger'),
                   pattern.pattern_literal((('smoke', 0.5,),)),
                   contexts.variable('ans2'),
                   pattern.pattern_literal('eosinophils_in_secretions'),
                   contexts.variable('ans3'),
                   contexts.variable('sum'),
                   contexts.variable('ans'),))
  
  bc_rule.bc_rule('vasomotor_rhinitis', This_rule_base, 'diagnosis',
                  vasomotor_rhinitis, None,
                  (pattern.pattern_literal('vasomotor'),
                   contexts.variable('ans'),),
                  (),
                  (pattern.pattern_literal('perrenial_rhinitis'),
                   contexts.variable('ans1'),
                   pattern.pattern_literal('skin_test_for_allergen'),
                   pattern.pattern_literal((('negative', 0.9,),)),
                   pattern.pattern_literal('trigger'),
                   pattern.pattern_literal((('smoke', 0.5,),)),
                   contexts.variable('ans2'),
                   contexts.variable('sum'),
                   contexts.variable('ans'),))
  
  bc_rule.bc_rule('score_answered', This_rule_base, 'score',
                  score_answered, None,
                  (contexts.variable('question'),
                   contexts.variable('answers'),
                   pattern.pattern_tuple((contexts.variable('score'), contexts.variable('max_score'), pattern.pattern_literal(None),), None),),
                  (),
                  (contexts.variable('ans'),
                   contexts.variable('max_score'),
                   contexts.variable('score'),))
  
  bc_rule.bc_rule('score_unanswered', This_rule_base, 'score',
                  score_unanswered, None,
                  (contexts.variable('question'),
                   contexts.variable('answers'),
                   pattern.pattern_tuple((pattern.pattern_literal(0), contexts.variable('max_score'), contexts.variable('question'),), None),),
                  (),
                  (contexts.variable('max_score'),))
  
  bc_rule.bc_rule('score_not_match', This_rule_base, 'score_not',
                  score_not_match, None,
                  (contexts.anonymous('_question'),
                   contexts.variable('ans'),
                   contexts.variable('score'),
                   pattern.pattern_tuple((pattern.pattern_literal(0), contexts.variable('score'), pattern.pattern_literal(None),), None),),
                  (),
                  (contexts.variable('ans'),))
  
  bc_rule.bc_rule('score_not_no_match', This_rule_base, 'score_not',
                  score_not_no_match, None,
                  (contexts.anonymous('_question'),
                   contexts.anonymous('_ans'),
                   contexts.variable('score'),
                   pattern.pattern_tuple((contexts.variable('score'), contexts.variable('score'), pattern.pattern_literal(None),), None),),
                  (),
                  (contexts.anonymous('_different_ans'),))
  
  bc_rule.bc_rule('score_not_unanswered', This_rule_base, 'score_not',
                  score_not_unanswered, None,
                  (contexts.variable('question'),
                   contexts.anonymous('_ans'),
                   contexts.variable('score'),
                   pattern.pattern_tuple((pattern.pattern_literal(0), contexts.variable('score'), contexts.variable('question'),), None),),
                  (),
                  ())

from naimath.engine.helpers import cf_threshold, sum_cf, mult_cf

Krb_filename = '../pulmonary.krb'
Krb_lineno_map = (
    ((16, 20), (4, 4)),
    ((22, 29), (6, 6)),
    ((30, 37), (7, 7)),
    ((38, 45), (8, 8)),
    ((46, 53), (9, 9)),
    ((54, 61), (10, 10)),
    ((62, 69), (11, 11)),
    ((72, 72), (12, 12)),
    ((74, 74), (13, 13)),
    ((89, 93), (18, 18)),
    ((95, 102), (20, 20)),
    ((103, 110), (21, 21)),
    ((111, 118), (22, 22)),
    ((119, 126), (23, 23)),
    ((127, 134), (24, 24)),
    ((135, 142), (25, 25)),
    ((145, 145), (26, 26)),
    ((147, 147), (27, 27)),
    ((162, 166), (30, 30)),
    ((168, 175), (32, 32)),
    ((176, 183), (33, 33)),
    ((186, 186), (34, 34)),
    ((188, 188), (35, 35)),
    ((203, 207), (38, 38)),
    ((209, 216), (40, 40)),
    ((217, 224), (41, 41)),
    ((225, 232), (42, 42)),
    ((233, 240), (43, 43)),
    ((243, 243), (44, 44)),
    ((245, 245), (45, 45)),
    ((260, 264), (49, 49)),
    ((266, 272), (51, 51)),
    ((273, 280), (52, 52)),
    ((283, 283), (53, 53)),
    ((287, 287), (54, 54)),
    ((305, 309), (58, 58)),
    ((311, 317), (60, 60)),
    ((318, 325), (61, 61)),
    ((326, 333), (62, 62)),
    ((334, 341), (63, 63)),
    ((344, 344), (65, 65)),
    ((348, 348), (66, 66)),
    ((366, 370), (69, 69)),
    ((372, 378), (71, 71)),
    ((379, 386), (72, 72)),
    ((387, 394), (73, 73)),
    ((397, 397), (75, 75)),
    ((401, 401), (76, 76)),
    ((419, 423), (82, 82)),
    ((425, 430), (84, 84)),
    ((431, 436), (85, 85)),
    ((437, 441), (86, 91)),
    ((444, 444), (92, 92)),
    ((448, 448), (93, 93)),
    ((466, 470), (96, 96)),
    ((474, 474), (98, 98)),
    ((490, 494), (101, 101)),
    ((496, 501), (103, 103)),
    ((502, 507), (104, 104)),
    ((520, 524), (107, 107)),
    ((526, 531), (109, 109)),
    ((532, 537), (110, 110)),
    ((550, 554), (113, 113)),
)
